# Maintainer: Bruno Pagani <archange@archlinux.org>
# Contributor: Baptiste Jonglez <baptiste--aur at jonglez dot org>

pkgname=jami-daemon
pkgver=20230323
pkgrel=1
pkgdesc="Free and universal communication platform which preserves the users’ privacy and freedoms (daemon component)"
arch=(loong64 x86_64)
url="https://jami.net"
license=(GPL3)
groups=(jami)
depends=(glibc opendht gnutls nettle libgit2 libsecp256k1 ffmpeg speexdsp
         fmt yaml-cpp jsoncpp zlib alsa-lib libpulse jack libupnp libnatpmp
         openssl webrtc-audio-processing systemd-libs libarchive)
# portaudio needs a not-yet-upstream patch https://git.jami.net/savoirfairelinux/jami-daemon/-/issues/650
makedepends=(git cmake perl asio msgpack-c msgpack-cxx restinio systemd meson)
checkdepends=(cppunit)
_commit=c5bbd21e1c125f4012e2f88f485816a438ae62a7
_pjprojectver=e4b83585a0bdf1523e808a4fc1946ec82ac733d0
source=(git+https://git.jami.net/savoirfairelinux/${pkgname}.git#commit=${_commit}
        https://github.com/savoirfairelinux/pjproject/archive/${_pjprojectver}/pjproject-${_pjprojectver}.tar.gz)
noextract=(pjproject-${_pjprojectver}.tar.gz)
sha512sums=('SKIP'
            '617f3a5cd38423a429b17553bb73ad467694ce3e899a788aaf20d0540f6a6904c06a2fc35bf9e0d1c9ab07ba821ca0e1d6d2b002e473bfa9850ff6f8b96447c0')

pkgver() {
  cd ${pkgname}
  TZ=UTC git show -s --pretty=%cd --date=format-local:%Y%m%d HEAD
}

prepare() {
  cd ${pkgname}
  cp ../pjproject-${_pjprojectver}.tar.gz contrib/tarballs
  mkdir contrib/native

  git cherry-pick -n 315b5fbf546712f22a7b03ca750257bc92263a91 # Support opendht 2.5
}

build() {
  cd ${pkgname}/contrib/native
  export CXXFLAGS+=' -fpermissive'
  ../bootstrap \
      --disable-downloads \
      --disable-all \
      --enable-pjproject
  make DEPS_pjproject=

  cd "${srcdir}"
  arch-meson ${pkgname} build -D interfaces=library -D opensl=disabled -D portaudio=disabled \
                              $( ((CHECKFUNC)) && echo -D tests=true) \
                              -D pkg_config_path="${PWD}/${pkgname}/contrib/$(cc -dumpmachine)/lib/pkgconfig"
  meson compile -C build
}

#check() {
# https://git.jami.net/savoirfairelinux/jami-daemon/-/issues/649
#  meson test -C build --print-errorlogs
#}

package() {
  meson install -C build --destdir "${pkgdir}"
}
